//
//  ThirdlubJumpViewController.m
//  Project_B
//
//  Created by dllo on 15/11/25.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "ThirdlubJumpViewController.h"
#import "ThirdlubModel.h"  //  标题model
#import "MBProgressHUD.h"  //  轮播
#import "MJRefresh.h"  //  刷新
#import <objc/runtime.h> 
#import "UIImageView+WebCache.h"  //  网络图片
#import "UIImage+GIF.h"
#import "AFNetworkHandler.h"//   网络请求
#import "Thirdlub_DiscussionCollectionViewCell.h" //  讨论区
#import "Thirdlub_routeCollectionViewCell.h"  //  线路活动
#import "FirstTopViewViewController.h"


@interface ThirdlubJumpViewController () < UICollectionViewDataSource , UICollectionViewDelegate , discussDelegate , RoDelegate >

@property (nonatomic, retain) NSDictionary *headerDic;
@property (nonatomic, retain) NSMutableArray *discussionArr;
@property (nonatomic, retain) NSMutableArray *rootArr;
@property (nonatomic, retain) UIView *icon;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UIView *header;
@property (nonatomic, retain) NSMutableArray *DiscussionNormal;
@property (nonatomic, retain) NSMutableArray *DiscussionTop;
@property (nonatomic, retain) MBProgressHUD * HUD;

@end

@implementation ThirdlubJumpViewController


- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSLog(@"~~~%@", self.model.title);
    
    [self prepareHeaderData];
    [self creatcollectionView];
#pragma mark -
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //小菊花
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:NO];
    self.HUD.animationType = MBProgressHUDAnimationFade;
}

#pragma mark - 准备头部数据
- (void)prepareHeaderData
{
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v2/club/detail/%@/?club_id=%@", self.idString, self.idString];
    [AFNetworkHandler getUrl:str cookie:nil result:^(id result) {
        self.headerDic = result[@"data"];
        [self headerView];
        [self leftButton];
        [self prepareDiscussionData];
    }];

}
#pragma mark - 页面将要出现
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    [self leftButton];
}

#pragma mark - 左按钮
- (void)leftButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setImage:[UIImage imageNamed:@"iconfont-fanhui@2x"] forState:UIControlStateNormal];
    button.tintColor = [UIColor whiteColor];
    button.frame = CGRectMake(20, 20, 40, 40);
    [button addTarget:self action:@selector(leftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

#pragma mark - 按钮方法
- (void)leftButtonAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - tableHeaderView
- (void)headerView
{
    self.header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 190)];
    [self.view addSubview:self.header];

    //背景图
    UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 150)];
    [backgroundImage setImage:[UIImage imageNamed:@"heise.jpg"]];
    [self.header addSubview:backgroundImage];

    //图标
    UIImageView *headicon = [[UIImageView alloc] initWithFrame:CGRectMake((WIDTH - 60) / 2, 40, 60, 60)];
    [headicon sd_setImageWithURL:[NSURL URLWithString:self.headerDic[@"images"]]];
    headicon.layer.cornerRadius = 30;
    headicon.clipsToBounds = YES;
    [self.header addSubview:headicon];
  
    //聚乐部名
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, headicon.frame.origin.y + headicon.frame.size.height + 10, WIDTH, 17)];
    label.font = [UIFont boldSystemFontOfSize:17];
    label.textAlignment = NSTextAlignmentCenter;
    if ([self.headerDic[@"title"] containsString:@"LTZ"]) {
        NSString *str = [self.model.title stringByReplacingOccurrencesOfString:@"LTZ" withString:@"LTZ户外"];
        label.text = str;
    } else {
        label.text = self.headerDic[@"title"];
    }
    label.textColor = [UIColor whiteColor];
    [self.header addSubview:label];
 
    UILabel * topic_title = [[UILabel alloc] initWithFrame:CGRectMake(0, headicon.frame.origin.y + headicon.frame.size.height + 30, WIDTH, 17)];
    topic_title.textAlignment = NSTextAlignmentCenter;
    topic_title.font = [UIFont boldSystemFontOfSize:12];
    topic_title.text = self.model.topic_title;
    [self.header addSubview:topic_title];
    topic_title.textColor = [UIColor whiteColor];
    
    //白条
    UIView *white = [[UIView alloc] initWithFrame:CGRectMake(0, backgroundImage.frame.size.height, WIDTH, 40)];
    white.backgroundColor = [UIColor clearColor];
    [self.header addSubview:white];

    //讨论区按钮
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(0, 0, WIDTH / 2, 40);
    button1.tag = 100;
    [button1 setTitle:@"讨论区" forState:UIControlStateNormal];
    button1.tintColor = [UIColor grayColor];
    [button1 addTarget:self action:@selector(discussionAction:) forControlEvents:UIControlEventTouchUpInside];
    [white addSubview:button1];
    //绿条
    self.icon = [[UIView alloc] initWithFrame:CGRectMake(0, 38, WIDTH / 2, 3)];
    self.icon.backgroundColor = [UIColor colorWithRed:60.0 / 255 green:149.0 / 255 blue:58.0 / 255 alpha:1];
    [button1 addSubview:self.icon];

    //线路按钮
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(WIDTH / 2, 0, WIDTH / 2, 40);
    button2.tag = 101;
    button2.tintColor = [UIColor grayColor];
    [button2 setTitle:@"线路活动" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(discussionAction:) forControlEvents:UIControlEventTouchUpInside];
    [white addSubview:button2];
    
}

#pragma mark - 方法按钮
- (void)discussionAction:(UIButton *)btn
{
    if (btn.tag == 100) {
        self.icon.frame = CGRectMake(0, 38, WIDTH / 2, 2);
        [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    if (btn.tag != 100) {
        self.icon.frame = CGRectMake(WIDTH / 2, 38, WIDTH / 2, 2);
        [self.collectionView setContentOffset:CGPointMake(WIDTH, 0) animated:YES];
    }
}

#pragma mark - 滚动方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
#pragma mark -
    //文字 选中
    if ((self.collectionView.contentOffset.x / WIDTH ) * 100 != 100) {
        self.icon.frame = CGRectMake(0, 38, WIDTH / 2, 2);
    }//不选中
    if ((self.collectionView.contentOffset.x / WIDTH) * 100 == 100) {
        self.icon.frame = CGRectMake(WIDTH / 2, 38, WIDTH / 2, 2);
    }
    
}

#pragma mark - 创建collectionView
- (void) creatcollectionView
{
    self.navigationController.navigationBar.translucent = NO;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(WIDTH, HEIGHT - 190);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 190, WIDTH, HEIGHT - 190) collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[Thirdlub_DiscussionCollectionViewCell class] forCellWithReuseIdentifier:@"equipmentItem"];
    [self.collectionView registerClass:[Thirdlub_routeCollectionViewCell class] forCellWithReuseIdentifier:@"sportsItem"];
    
}

#pragma mark - collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        Thirdlub_DiscussionCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"equipmentItem" forIndexPath:indexPath];
        item.top = self.DiscussionTop;
        item.normal = self.DiscussionNormal;
        item.Delegate = self;
        item.club_id = self.idString;
        [item reloadData];
        return item;
    } else {
        Thirdlub_routeCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"sportsItem" forIndexPath:indexPath];
        item.rootArr = self.rootArr;
        item.delelgate = self;
        [item reloadData];
        return item;
    }
}

#pragma mark - 协议传值
- (void)discussionJumpVC:(NSString *)topic_id
{
    NSLog(@"fir:%@", topic_id);
    FirstTopViewViewController *columnVC = [[FirstTopViewViewController alloc] init];
    columnVC.idString = topic_id;
    [self.navigationController pushViewController:columnVC animated:YES];
}

- (void)RoJumpVC:(NSString *)topic_id
{
    NSLog(@"sec:%@", topic_id);
    FirstTopViewViewController *columnVC = [[FirstTopViewViewController alloc] init];
    columnVC.idString = topic_id;
    [self.navigationController pushViewController:columnVC animated:YES];
}

#pragma mark - 准备讨论区数据
- (void)prepareDiscussionData
{
    self.DiscussionNormal = [NSMutableArray array];
    self.DiscussionTop = [NSMutableArray array];
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v2/club/topiclist/%@/0/?page=0?category=0&club_id=%@&page=0", self.idString, self.idString];
    
    [AFNetworkHandler getUrl:str cookie:nil result:^(id result) {
        
        NSArray *arr = result[@"data"][@"topiclist"];
        for (NSDictionary *dic in arr) {
            if ([dic[@"stick"] isEqualToString:@"2"]) {
                [self.DiscussionNormal addObject:dic];
            } else {
                [self.DiscussionTop addObject:dic];
            }
        }
       
        [self prepareRootData];
        
    }];
    
}

#pragma mark - 准备线路数据
- (void)prepareRootData
{
    self.rootArr = [NSMutableArray array];
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v2/club/topiclist/%@/1/?page=0?category=1&club_id=%@&page=0", self.idString, self.idString];
    [AFNetworkHandler getUrl:str cookie:nil result:^(id result) {
       
        //移除小菊花
        [self.HUD removeFromSuperview];
        self.rootArr = result[@"data"][@"topiclist"];
        [self.collectionView reloadData];
       
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
